package com.eddie.stack;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Stack;

public class PolandNotation {

    public static void main(String[] args) {
        //完成一个中缀表达式转后缀表达式
        String expression = "1+((2+3)*4)-5";
        List<String> infixExpressionList = toInfixExpressionList(expression);
        //取出后缀表达式
        List<String> strings = parseSuffixExpressionList(infixExpressionList);
        int calculate = calculate(strings);
        System.out.println(calculate);
        //先定义逆波兰表达式
        //为了方便，逆波兰表达式的数字和符号使用空格隔开
//        String suffixExpression = "3 4 + 5 * 6 -";
//        List<String> listString = getListString(suffixExpression);
//        int calculate = calculate(listString);
//        System.out.println(calculate);

    }

    /**
     * 将得到的中缀表达式对应的List =》后缀表达式对应的List
     */
    public static List<String> parseSuffixExpressionList(List<String> ls) {
        //定义两个栈

        Stack<String> s1 = new Stack<>();
        //说明：因为s2这个栈在转换的过程中，没有pop操作，而且后面需要逆序输出
        //直接使用List代替
        ArrayList<String> s2 = new ArrayList<>();
        for (String item : ls) {
            if (item.matches("\\d+")) {
                s2.add(item);
            } else if ("(".equals(item)) {
                s1.push(item);
            } else if (")".equals(item)) {
                while (!"(".equals(s1.peek())) {
                    s2.add(s1.pop());
                }
                //将括号弹出栈，消除小括号
                s1.pop();
            } else {
                //当s1的优先级小于等于栈顶运算符的优先级
                while (s1.size() != 0 && Operation.getVal(s1.peek()) >= Operation.getVal(item)) {
                    s2.add(s1.pop());
                }
                s1.push(item);
            }
        }
        while (s1.size() != 0) {
            s2.add(s1.pop());
        }
        return s2;
    }

    /**
     * 返回运算符的优先级，由程序员来决定
     * 数字越大优先级越高
     */
    public static int priority(int opr) {
        if (opr == '*' || opr == '/') {
            return 1;
        } else if (opr == '+' || opr == '-') {
            return 0;
        }
        return -1;
    }

    /**
     * 将中缀表达式转成对应的list
     */
    public static List<String> toInfixExpressionList(String s) {
        //定义List，存放中缀表达式对应的内容
        List<String> ls = new ArrayList<>();
        StringBuilder str = new StringBuilder();
        char[] charArray = s.toCharArray();
        for (int i = 0, charArrayLength = charArray.length; i < charArrayLength; i++) {
            char c = charArray[i];
            if (c < 48 || c > 57) {
                ls.add("" + c);
            } else { //如果是一个数需要考虑多位数的问题
                str.append(c);
                while (i < charArrayLength - 1 && (charArray[i + 1] >= 48 && charArray[i + 1] <= 57)) {
                    str.append(charArray[i + 1]);
                    i++;
                }
                ls.add(str.toString());
                str = new StringBuilder();
            }
        }
        return ls;
    }

    /**
     * 将一个逆波兰表达式，依次将数据和运算符放入ArrayList
     */
    public static List<String> getListString(String suffixExpression) {
        //将suffix分割
        String[] split = suffixExpression.split(" ");
        return Arrays.asList(split);
    }

    /**
     * 完成对逆波兰表达式的运算
     */
    public static int calculate(List<String> ls) {
        //创建栈，一个即可
        Stack<String> stack = new Stack<>();
        for (String item : ls) {
            //使用正则表达式取出数
            //匹配多位数
            if (item.matches("\\d+")) {
                stack.push(item);
            } else {
                //弹出取两个并运算,在入栈
                int num2 = Integer.parseInt(stack.pop());
                int num1 = Integer.parseInt(stack.pop());
                int res = 0;
                res = cal(num1, num2, item.charAt(0));
                stack.push(String.valueOf(res));
            }
        }
        return Integer.parseInt(stack.pop());
    }


    public static int cal(int num1, int num2, char opr) {
        //存放计算的结果
        int res = 0;
        switch (opr) {
            case '+':
                res = num2 + num1;
                break;
            case '-':
                res = num2 - num1;
                break;
            case '*':
                res = num2 * num1;
                break;
            case '/':
                res = num2 / num1;
                break;
            default:
                throw new IllegalStateException("Unexpected value: " + opr);
        }
        return res;
    }

}

class Operation {
    private static int ADD = 1;
    private static int SUB = 1;
    private static int MUL = 2;
    private static int DIV = 2;

    //返回对应的优先级
    public static int getVal(String opr) {
        int result = 0;
        switch (opr) {
            case "+":
                result = ADD;
                break;
            case "-":
                result = SUB;
                break;
            case "*":
                result = MUL;
                break;
            case "/":
                result = DIV;
                break;
            default:
                System.out.println("不存在改运算符");
                break;
        }
        return result;
    }
}
